package cn.ekuma.epos.datalogic.define.dao.erp.subject;

import com.openbravo.bean.CustomerCategory;
import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.bean.erp.subject.SubjectCustomerCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.key.KeyBuilder;

import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import e.odbo.data.dao.ModifiedLogDAO;
import e.odbo.data.bean.I_ModifiedLogBean;
import cn.ekuma.epos.datalogic.define.dao.CustomerCategoryDAO;
import cn.ekuma.epos.db.table.erp.subject.I_Subject;

public class SubjectCustomerCategoryDAO extends ModifiedLogDAO<SubjectCustomerCategory>{
	CustomerCategoryDAO customerCategoryDAO;
	public SubjectCustomerCategoryDAO(I_Session s,CustomerCategoryDAO customerCategoryDAO) {
		super(s);
		this.customerCategoryDAO=customerCategoryDAO;
	}

	public SubjectCustomerCategoryDAO(I_Session s) {
		super(s);
	}

	@Override
	public SubjectCustomerCategory readValues(DataRead dr,SubjectCustomerCategory obj)
			throws BasicException {
		if(obj==null)
			obj=new SubjectCustomerCategory();
		obj.setId(dr.getString(1));
		obj.setSubectId(dr.getString(2));
		obj.setCustomerCategoryId(dr.getString(3));
		if(customerCategoryDAO!=null)
		   obj.setCustomerCategory(customerCategoryDAO.find(KeyBuilder.getKey(dr.getString(3))));
		obj.setLastModified(dr.getTimestamp(4));
		return null;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(
                "SUBJECT_CUSTOMER_CATEGORIES",
                new Field[]{
                   new Field("ID",Datas.STRING, Formats.STRING),
                   new Field("SUBJECT",Datas.STRING, Formats.STRING,Subject.class),
                   new Field("CUSTOMERCATEGORIE",Datas.STRING, Formats.STRING,CustomerCategory.class),
                   new Field(I_ModifiedLogBean.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                 },
                new int[]{0}
                );
	}

	@Override
	public void writeInsertValues(DataWrite dp, SubjectCustomerCategory obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getSubectId());
		dp.setString(3,obj.getCustomerCategoryId());
		dp.setTimestamp(4,obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return SubjectCustomerCategory.class;
	}

}
